package gameEngine.agents.distrebution.representation;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ui.flowManager.expirimentProperties.AgentRepresentation;

import Data.ConfigurationParameters;

public abstract class DistributionRepresentation {

	protected String distributionName = null;

	
	public String getDistributionName() {
		return distributionName;
	}
	
	/**
	 * Designed for create element distribution with
	 *   her data
	 * @return Element
	 */
	public Element getXMLElement(Document doc)
	{
		
		//create Distribtion element with name && lambda
		Element distribution = doc.createElement(ConfigurationParameters.Distribution.DISTRIBUTION);
		//name
		Element name = doc.createElement("Name");
		name.appendChild(doc.createTextNode(this.distributionName));

		distribution.appendChild(name);
		
		//for more elements of distribution
		setExtraElement(doc, distribution);
		
		return distribution;
	}
	
		
	/**
	 * For more elements of distribution 
	 * @param doc
	 * @param strategy
	 */
	public abstract void setExtraElement(Document doc, Element distribution);
	
	public static void openXMLElement(Document doc, Element distElement, AgentRepresentation agRepresentation, String name)
	{
		//case the Exponential	
		if(name.equals(ConfigurationParameters.Distribution.EXPONENTIAL))
		{	
			ExponentialRepresentation exRepresentation = new ExponentialRepresentation(name);
			//set Extra data from the fields to the distribution
			exRepresentation.openExtraDistributionData(doc, distElement);
			agRepresentation.setDistributionType(exRepresentation);
		}
		//case Uniform
		else if(name.equals(ConfigurationParameters.Distribution.UNIFORM))
		{
			UniformRepresentation unRepresentation = new UniformRepresentation(name);
			//set Extra data from the fields to the distribution
			unRepresentation.openExtraDistributionData(doc, distElement);
			agRepresentation.setDistributionType(unRepresentation);
		}
		//case Fixed
		else if(name.equals(ConfigurationParameters.Distribution.FIXED))
		{
			FixedRepresentation fixRepresentation =  new FixedRepresentation(name);
			//set Extra data from the fields to the distribution
			fixRepresentation.openExtraDistributionData(doc, distElement);
			agRepresentation.setDistributionType(fixRepresentation);
		}
		//case Normal
		else if(name.equals(ConfigurationParameters.Distribution.NORMAL))
		{
			NormalRepresentation normalRepresentation = new NormalRepresentation(name);
			//set Extra data from the fields to the distribution
			normalRepresentation.openExtraDistributionData(doc, distElement);
			
			agRepresentation.setDistributionType(normalRepresentation);
		}
	}
	
	public abstract void openExtraDistributionData(Document doc, Element distElement);
	
	
	/**
	 * @param sTag tag name
	 * @param eElement
	 * @return return the tag value.
	 */
	 protected static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
	 
	        Node nValue = (Node) nlList.item(0);
	 
		return nValue.getNodeValue();
	 }
}
